package com.snake.spring.boot.smaple.retrofit.utils;

import com.sinosoft.RSAUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author cheng
 */
@Slf4j
public class Tools {
    private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHlD2SehFnI5S7030rs+oQ7D2+bVhmYtDr8RpzUHRmPrtT78TR7MUTt/fmxEeFUAnruRvjEkVgXxyY17fXWYi++DTb"
            + "Vp9lmFEI4AFZNxuhomozALSwpYnyD3/zkJaXPLzZ8pjRfHBag5DsuSmaeQrCsqDHuVYiTghtt9oLJ4r2XwIDAQAB";
    private static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIeUPZJ6EWcjlLvTfSuz6"
            + "hDsPb5tWGZi0OvxGnNQdGY+u1PvxNHsxRO39+bER4VQCeu5G+MSRWBfHJjXt9dZiL74"
            + "NNtWn2WYUQjgAVk3G6GiajMAtLClifIPf/OQlpc8vNnymNF8cFqDkOy5KZp5CsKyoMe5"
            + "ViJOCG232gsnivZfAgMBAAECgYBszjAXxHUA89IiZpjWoN2g1Sa8E17VVEpcf7L9hjzT7VZ"
            + "DB3JKEp0atRJzFWRcfZ5ombbipcAdllcJ5l04qzcM7iSAc6LiRQVJo5RSX36ry8/Ijkf2ZrBaju"
            + "3aFYGLhKcTtIFU6H5YOJc6LTtCF069c7SFWHbT5Nc3zdODL55qSQJBAPIdfH4DVWfrfZSMpQglknqQC/1"
            + "3MtJyeFyrI2Aj44m6ztmMrKz4iGeUpFyZWLzWwX9GW9zftWJ600dBA8vvFasCQQCPWrA5Jc0HhIWUwkQMsJDv"
            + "7Y/NXs9uzOukgmxI02H12A94hV5qsApXrocAp8x6J1EMP7B9yUp5q6HD2RLeWoYdAkBMAN+cuzUrSyx/3mrMFOdX"
            + "nDJ3XdzRXWfVR0/ylTGPgdn/Rv7JybcgNPQnsWe+1/ekGZUqz/sjsAT1AD7N3P7DAkAoOmPPdasmm+q8hxN/mCDkI39C"
            + "O4PsbPcHkE4A1cjcq4gT4yFOwR314xUhevUnplbZIBpQAiezIxXfFtBzs2WFAkAlRvmDMzpR0dukUECSyQbzVPpKMPz9RpF"
            + "j0+ZCu3L6JDR1xTMS5gj0IYWbAsWNZvjR1Si7Zdod5s2kk2GAkI8L";

    /**
     * @param reqJson
     * @return
     * @throws Exception
     */
    public static String encryptByPublicKey(String reqJson) {
        String sign = null;
        try {
            sign = RSAUtils.encryptByPublicKey(reqJson, publicKey);
            log.info("加密后的字符串为 {}", sign);
            return sign;
        } catch (Exception e) {
            log.error("encryptByPublicKey error occur", e);
        }
        return null;
    }


    public static void main(String[] args) {
        //获取流水号
        System.out.println(getRandomReqNo());
    }

    /**
     * 获取流水号(22位)
     *
     * @return
     */
    public static StringBuilder getRandomReqNo() {
        StringBuilder messageid = new StringBuilder();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String date = sdf.format(new Date());
        //拼接时间
        messageid.append(date);

        int length = 10;
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        //拼接流水号
        messageid.append(sb);
        return messageid;
    }

}
